/// Creator:      gaoruishan
/// CreatDate:    2025-06-24
/// Description:  外出管理  
Class Nur.MNISV3.Service.OutManage Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 获取病人某段时间内的外出数据
/// w ##class(Nur.MNISV3.Service.OutManage).GetOutDataV4("","2025-06-23","2025-06-23","8","20665","3","2","{""single"":"""",""multiple"":""1""}")
ClassMethod GetOutDataV4(episodeId, fromDate, toDate, locId, userId, wardId, hospitalId, extras)
{
    n (episodeId, fromDate, toDate, locId, userId,wardId,hospitalId,extras,%session)
    s $zt="errGetOutDataV4"
    s ^GRS("GetOutDataV4")=$lb(episodeId, fromDate, toDate, locId, userId,wardId,hospitalId,extras)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s fromDate=##class(websys.Conversions).DateHtmlToLogical(fromDate)
    s toDate=##class(websys.Conversions).DateHtmlToLogical(toDate)
    s patInfoList=##class(%ListOfDataTypes).%New()
    i episodeId'="" {//单患者
        s patinfo=##class(%ArrayOfDataTypes).%New()
        //基本患者信息
        s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
        s outData=..GetOutDataObj(episodeId,fromDate,toDate)
        d patinfo.SetAt($g(outData),"outData")
        d patInfoList.Insert(patinfo)
    }else {
        //调用纯净版患者列表，
        s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
        d patListObj.Execute(wardId,userId,"Ward",locId)
        while(patListObj.Next()){
            s patinfo=##class(Nur.MNISV3.Common.Utils).ResultSet(patListObj)
            s episodeId=patListObj.GetDataByName("episodeId")
            s outData=..GetOutDataObj(episodeId,fromDate,toDate)
            d patinfo.SetAt($g(outData),"outData")
            //基本患者信息
            s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
            d patInfoList.Insert(patinfo)
        }
    }
    //列配置项
    s traceListObj=##class(%ResultSet).%New("Nur.NIS.Service.Trace.Manage:GetTraceConfig")
    d traceListObj.Execute(hospitalId)
    s outItems=##class(Nur.MNISV3.Common.Utils).ResultSetToList(traceListObj)
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    d json.SetAt($g(outItems),"outItems") 
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetOutDataV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

/// 保存外出记录
/// w ##class(Nur.MNISV3.Service.OutManage).SaveOutV4(
ClassMethod SaveOutV4(episodeId, typeId, id, outDate, outTime, accompanyingPerson, returnDate, returnTime, notes)
{
    n (episodeId, typeId, id, outDate, outTime, accompanyingPerson, returnDate, returnTime, notes,%session)
    s $zt="errSaveOutV4"
    s ^GRS("SaveOutV4")=$lb(episodeId, typeId, id, outDate, outTime, accompanyingPerson, returnDate, returnTime, notes)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s data=##class(Nur.Tools).RapidAssembly("episodeID^typeDR^id^outDateTime^entourage^returnDateTime^remarks",
        episodeId,typeId,id, $s(outTime'="":outDate_" "_outTime,1:""),
        accompanyingPerson, $s(returnTime'="":returnDate_" "_returnTime,1:""), notes)
    s jsonStr=##class(Nur.MNISV3.Common.JSON).Encode(data)
    s ret=##class(Nur.NIS.Service.Trace.Manage).AddOrUpdatePatientTrace(jsonStr)
    i ret'=0 s Msg=ret,Status="-1"
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errSaveOutV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

/// 修改外出状态 
/// w ##class(Nur.MNISV3.Service.OutManage).SaveOutV4(
ClassMethod ChangeOutStatusV4(id, motion)
{
    n (id, motion,%session)
    s $zt="errChangeOutStatusV4"
    s ^GRS("ChangeOutStatusV4")=$lb(id, motion)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    q:(id="") ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("请选择一条记录","100021","-1")
    s obj=##class(Nur.NIS.PatientTrace).%OpenId(id)
    q:('$isobject(obj)) ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("数据错误","100021","-1")
    s cancelled=obj.voidFlag
    d obj.%Close()
    q:($g(motion)="unuse")&&(cancelled=1) ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("该记录已作废","100021","-1")
    q:($g(motion)="reuse")&&(cancelled'=1) ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("该记录未作废","100021","-1")
    s ret=##class(Nur.NIS.Service.Trace.Manage).ToggleVoidPatientTrace(id)
    i ret'=0 s Msg=ret,Status="-1"
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errChangeOutStatusV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

ClassMethod GetOutDataObj(episodeId, fromDate, toDate) As %String
{
    n (episodeId,fromDate, toDate,%session)
    s stDate=fromDate,edDate=toDate
    s list=##class(%ListOfDataTypes).%New()
    s dateF=edDate+1 f  s dateF=$o(^Nur.NIS.PatientTraceI("episodeId",episodeId,dateF),-1) q:(dateF="")||(dateF<stDate)  d
    .s idF="" f  s idF=$o(^Nur.NIS.PatientTraceI("episodeId",episodeId,dateF,idF),-1) q:(idF="")  d
    ..s data=$g(^Nur.NIS.PatientTraceD(idF))
    ..q:($lg(data,10)=1) //已删除
    ..s typeId=$lg(data,3)
    ..s outDate=$lg(data,4)
    ..s outTime=$lg(data,5)
    ..s outRecDate=$lg(data,12)
    ..s outRecTime=$lg(data,13)
    ..s outRecUser=$lg(data,11)
    ..i (outDate'="") s outDate=$zd(outDate,3)
    ..i (outTime'="") s outTime=$zt(outTime,2)
    ..i (outRecDate'="") s outRecDate=$zd(outRecDate,3)
    ..i (outRecTime'="") s outRecTime=$zt(outRecTime,2)
    ..i (outRecUser'="") s outRecUser=$p($g(^SSU("SSUSR",outRecUser)),"^",2)
    ..s returnDate=$lg(data,6)
    ..s returnTime=$lg(data,7)
    ..s returnRecDate=$lg(data,17)
    ..s returnRecTime=$lg(data,18)
    ..s returnRecUser=$lg(data,16)
    ..i (returnDate'="") s returnDate=$zd(returnDate,3)
    ..i (returnTime'="") s returnTime=$zt(returnTime,2)
    ..i (returnRecDate'="") s returnRecDate=$zd(returnRecDate,3)
    ..i (returnRecTime'="") s returnRecTime=$zt(returnRecTime,2)
    ..i (returnRecUser'="") s returnRecUser=$p($g(^SSU("SSUSR",returnRecUser)),"^",2)
    ..s accompanyingPerson=$lg(data,8)
    ..s notes=$lg(data,9)
    ..s status=$s($lg(data,26)=1:"作废",1:"")
    ..s source=$s($lg(data,27)'="":"系统带入",1:"手动录入")
    ..s obj=##class(%ArrayOfDataTypes).%New()
    ..d obj.SetAt(outDate,"outDate")
    ..d obj.SetAt(outTime,"outTime")
    ..d obj.SetAt(outRecDate,"outRecDate")
    ..d obj.SetAt(outRecTime,"outRecTime")
    ..d obj.SetAt(outRecUser,"outRecUser")
    ..d obj.SetAt(returnDate,"returnDate")
    ..d obj.SetAt(returnTime,"returnTime")
    ..d obj.SetAt(returnRecDate,"returnRecDate")
    ..d obj.SetAt(returnRecTime,"returnRecTime")
    ..d obj.SetAt(returnRecUser,"returnRecUser")
    ..d obj.SetAt(accompanyingPerson,"accompanyingPerson")
    ..d obj.SetAt(status,"status")
    ..d obj.SetAt(source,"source")
    ..d obj.SetAt(typeId,"typeID")
    ..d obj.SetAt(episodeId,"episodeId")
    ..i $g(typeId)'="" s typeDesc=##class(CF.NUR.NIS.TraceEvent).%OpenId(typeId).type
    ..d obj.SetAt($g(typeDesc),"typeDesc")
    ..d obj.SetAt(idF,"ID")
    ..d obj.SetAt(notes,"notes") 
    ..d list.Insert(obj)
    q list
}

}
